/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.common.constant.MannedStatusEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.vo.*;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.MannedService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:li.yao@tbit.com.cn">yao</a>
 * @version 4.2.0
 * @since 2024/9/10 11:16
 */
@RestController
@RequestMapping("/manned")
public class MannedController {

    @Resource
    private MannedService mannedService;


    @GetMapping("/getPage")
    public JsonResult getPage(MannedPageReqVO reqVO) {
        PageInfo<MannedRespVO> page = mannedService.getPage(reqVO);
        return JsonResult.ok(PageResult.convertPage(page));
    }

    @PostMapping("/bind")
    @OperateLog(module = ModuleEnum.MAINMANNED, methods = MethodEnum.MANNED_BIND)
    public JsonResult bind(@RequestBody MannedBindingReqVO reqVO) {
        mannedService.binding(reqVO);
        return JsonResult.ok();
    }

    @PostMapping("/unBind")
    @OperateLog(module = ModuleEnum.MAINMANNED, methods = MethodEnum.MANNED_UNBIND)
    public JsonResult unBind(@RequestBody MannedBindingReqVO reqVO) {
        mannedService.binding(reqVO);
        return JsonResult.ok();
    }

    @PostMapping("/importExcel")
    @OperateLog(module = ModuleEnum.MAINMANNED, methods = MethodEnum.MANNED_IMPORT)
    public JsonResult importExcel(MannedExcelReqVO reqVO) {
        return JsonResult.ok(mannedService.importExcel(reqVO));
    }

    @GetMapping("/exportExcel")
    public JsonResult exportExcel(MannedPageReqVO reqVO) {
        List<MannedRespVO> result = mannedService.getList(reqVO);
        result.forEach(m -> m.setStatusDesc(MannedStatusEnum.getDesc(m.getStatus())));
        return JsonResult.ok(new ExcelUtil<>(MannedRespVO.class).exportExcel(result, "载人设备列表").getData());
    }

    @GetMapping("/downloadTemplate")
    public JsonResult downloadTemplate() {
        // 下载模板文件
        return JsonResult.ok(new ExcelUtil<>(MannedBindingExcelVO.class).exportExcel(new ArrayList<>(), "载人设备导入模板").getData());
    }


}
